{extend name="base" /}
{block name="body"}
<link rel="stylesheet" href="/static/dist/basic.min.css" />
<link rel="stylesheet" href="/static/dist/dropzone.min.css" />

<div class="layui-card" style="width: 1000px;height: 1600px">
    <div class="layui-card-body">
        <form action="{:request()->url()}" class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">出单方名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" value="{$data.name|default=''}" autocomplete="off" placeholder="请输入出单方名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择机构</label>
                <div class="layui-input-block">
                    <select name="aid" lay-verify="required" id="agency" lay-filter="agency">
                        {if condition="!empty($aid)"}
                        <option value="{$aid}">{$aname}</option>
                        {else}
                        {volist name='agency' id='vo'}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                        {/if}

                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择保险公司</label>
                <div class="layui-input-block">
                    <select name="cid" lay-verify="required" id="company">
                        {if condition="!empty($cid)"}<option value="{$cid}">{$cname}</option>
                        {else}
                        {volist name='company' id='vo'}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                        {/if}

                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">出单方工号</label>
                <div class="layui-input-block">
                    <input type="text" name="code" value="{$data.code|default=''}" autocomplete="off" placeholder="请输入出单方工号" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">出单方密码</label>
                <div class="layui-input-block">
                    <input type="text" name="password" value="{$data.password|default=''}" autocomplete="off" placeholder="请输入出单方密码" class="layui-input">
                </div>
            </div>

            <input name="isedit" value="{:input('isedit')}" hidden="hidden">
            {if condition="input('isedit') == 1"}
            <input value="{$data.id}" hidden="hidden" name="id">
            {/if}
        </form>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="i" id="sub" >保存</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script src="/static/js/clipboard.js"></script>
<script src="/static/dist/dropzone.min.js"></script>
<script type="text/javascript" charset="utf-8" src="/static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/static/ueditor/ueditor.all.js"> </script >

{/block}

{block name="js"}
<script>
    $('#sub').click(function(){
        if($('input[name="name"]').val() == ''){
            layer.open({
                content: '保险公司名称必填' //这里content是一个普通的String
            });
            return;
        }

        if($('input[name="aid"]').val() == ''){
            layer.open({
                content: '机构必选' //这里content是一个普通的String
            });
            return;
        }

        if($('input[name="cid"]').val() == ''){
            layer.open({
                content: '保险公司必选' //这里content是一个普通的String
            });
            return;
        }

        if($('input[name="password"]').val() == ''){
            layer.open({
                content: '出单方密码必填' //这里content是一个普通的String
            });
            return;
        }

        if($('input[name="code"]').val() == ''){
            layer.open({
                content: '出单方工号必填' //这里content是一个普通的String
            });
            return;
        }
        var data = $('form').serializeJson();
        $.ajax({
            url:'bill_add',
            data: data,
            method:'post',
            success:function(res){
                layer.open({
                    content: res.msg, //这里content是一个普通的String
                    end: function(){ //此处用于演示
                        if(res.code == 1){
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
                            parent.layer.close(index); //再执行关闭
                        }
                    }
                });
            }
        });
    })



    layui.use('form', function(){
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功

        form.on('select(agency)', function(data){
            $.ajax({
                url:'getCompany',
                data:{'id':data.value},
                method:'post',
                success:function(res){
                    $('#company').children().remove();
                    let data = JSON.parse(res);
                    let str = '';
                    for(let i in data){
                        str += ' <option value="'+data[i]['id']+'">'+data[i]['name']+'</option>'
                    }
                    $('#company').append(str);
                    form.render();
                }
            });
        });

    });


</script>
{/block}
